package org.cdivtc.exam.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.cdivtc.exam.entity.Students;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 * 学生基本信息表 Mapper 接口
 * </p>
 *
 * @author xiaogoudashuai
 * @since 2025-03-27
 */
@Mapper
public interface StudentsMapper extends BaseMapper<Students> {

    /*根据账号登录*/
    @Select("SELECT * FROM students WHERE account = #{account}")
    Students selectByAccount(@Param("account") String account);

    /*同时更新手机号和密码*/
    @Update("UPDATE students SET phone = #{newPhone}, password = #{newPassword} WHERE student_id = #{studentId}")
    int updatePhoneAndPassword(@Param("studentId") Long studentId,
                               @Param("newPhone") String newPhone,
                               @Param("newPassword") String newPassword);

    /*检查手机号是否被其他学生使用*/
    @Select("SELECT COUNT(*) > 0 FROM students WHERE phone = #{phone} AND student_id != #{excludeStudentId}")
    boolean existsByPhoneAndNotStudent(@Param("phone") String phone,
                                       @Param("excludeStudentId") Long excludeStudentId);

    @Select("SELECT COUNT(*) FROM students WHERE account = #{account}")
    boolean existsByAccount(@Param("account") String account);


}

